package com.hs.sfzy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hs.sfzy.entity.Article;
import com.hs.sfzy.entity.Articletype;
import com.hs.sfzy.entity.Systemuser;
import com.hs.sfzy.mapper.ArticleMapper;
import com.hs.sfzy.mapper.ArticletypeMapper;
import com.hs.sfzy.mapper.SystemuserMapper;
import com.hs.sfzy.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName ArticleServiceImpl
 * @Description TODO
 * @Author zgy
 * @Date 2024年10月21日 10:45
 * @Version 1.0
 */
@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private ArticletypeMapper articletypeMapper;
    @Autowired
    private SystemuserMapper systemuserMapper;

    @Override
    public int insert(Article article) {

        return articleMapper.insert(article);
    }

    @Override
    public int delete(int id) {
        return articleMapper.delete(id);
    }

    @Override
    public int update(Article article) {
        return articleMapper.update(article);
    }

    @Override
    public List<Article> selectAll(Article article) {

        return articleMapper.selectAll(article);
    }

    @Override
    public Article selectById(int id) {

        return articleMapper.selectById(id);
    }

    @Override
    public PageInfo<Article> selectByPageInfo(int pageNum, int pageSize, Article article) {
        PageHelper.startPage(pageNum,pageSize);
        List<Article> list = articleMapper.selectAll(article);
        // 遍历文章集合，根据每一个文章的typeid来查询对应的文章类型对象，并赋值给当前的对象
        for (Article at : list){
            Long typeId = at.getTypeId();
            Long teacherId = at.getTeacherId();
            Articletype articletype = articletypeMapper.selectById(typeId);
            Systemuser systemuser = systemuserMapper.selectById(teacherId);
            at.setArticletype(articletype);
            at.setSystemuser(systemuser);
        }
        PageInfo<Article> pageInfo = new PageInfo<Article>(list);
        return pageInfo;
    }
}
